﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Zadanie2
{
    public partial class Form1 : Form
    {
        CheckBox[][] boxes;
        logika logic;
        
        public Form1()
        {
            InitializeComponent();
            boxes = new CheckBox[20][];
            for (int i = 0; i < 20; i++)
            {
                boxes[i] = new CheckBox[20];
            }

            for (int i = 0; i < 20; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    boxes[i][j] = new CheckBox();
                    boxes[i][j].Size = new Size(50, 20);
                    boxes[i][j].Enabled = false;
                    boxes[i][j].Checked = true;
                    boxes[i][j].Location = new Point(10+50*i, 5+20*j);
                    boxes[i][j].Name = "Box "+(i+20*j+1).ToString();
                    boxes[i][j].Text = (i + 20 * j + 1).ToString();
                    this.Controls.Add(boxes[i][j]);
                }
            }
            boxes[0][0].Checked = false;
        }

        private void start_Click(object sender, EventArgs e)
        {
            logic = new logika((int)numThread.Value, (int)numericUpDown1.Value);
            logic.Changed += synchro;
        }

        public void synchro()
        {
            Invoke((MethodInvoker)delegate()
            {
                for (int i = 1; i < 400; i++)
                {
                    boxes[(i - 1) % 20][(i - 1) / 20].Checked = logic.checks[(i - 1) % 20][(i - 1) / 20];
                }
            });
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (logic != null) logic.killWatki();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (logic != null)
            {
                logic.killWatki();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 20; i++)
            {
                for (int j = 0; j < 20; j++)
                {
                    boxes[i][j].Checked = true;
                }
                boxes[0][0].Checked = false;
            }
        }
    }
}
